package com.vincce.sport.controller;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.vincce.sport.mapper.SysUserMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.web.filter.AccessControlFilter;
import org.apache.shiro.web.util.SavedRequest;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.vincce.sport.entity.SysUser;

/**
 * Created with IntelliJ IDEA.
 * User: xupeng
 * Date: 13-11-27
 * Time: 下午2:00
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/")
public class LoginController extends BaseController{

    @Resource
    SysUserMapper  sysUserMapper;


    /**
     * 跳转登录页
     * @return
     */
    @RequestMapping("/login")
    public String login(HttpServletRequest request) {
        SavedRequest savedRequest = WebUtils.getAndClearSavedRequest(request);
        if (savedRequest != null && savedRequest.getMethod().equalsIgnoreCase(AccessControlFilter.GET_METHOD)) {
            WebUtils.saveRequest(request);
        }
        return "backstage/login";
    }

    /**
     * 登录成功跳转主页
     * @return
     * @throws IOException 
     */
    @RequestMapping({"/home","/bkIndex"})
    public String home(Model model,HttpServletRequest request) throws IOException {
    	SysUser user = getCurrentUser();
        request.getSession().setAttribute("name", user.getUserName());
        request.getSession().setAttribute("myUserId",user.getId());
        this.sessionPutNodeResource(request);        //加载侧边条资源
        this.sessionPutResource(request);	//角色资源与全部资源放入session
        return "backstage/index";
    }
    
    @RequestMapping("/error")
    public String error(HttpServletRequest request){
        return "404";
    }
    
    @RequestMapping("/logout")
    public String logout(){
    	SecurityUtils.getSubject().logout();
    	return "backstage/login";
    }
}
